{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Vector Intersection\n",
    "To realize some form of geometrical connection between the pads of two given footprints, whether it is a parametric curve or otherwise, it is often useful to compute the vectors of each pad selected by the user. In doing so, an intersection point can be determined from two vector line equations that in turn provide information about how a copper track extends from a selected pad. Computing the pad-vectors is accomplished by subtracting the center point of the selected pad by the center point of the corresponding footprint.\n",
    "\n",
    "## Equation Setup\n",
    "Suppose we have an 0402 footprint with center point $(6.5, 3)$ and the user selects a pad on that same footprint with center point $(6.5, 3.017716535)$. A vector, $\\vec{P_0}$, can be computed from the center of the footprint to the center of the pad:\n",
    "\n",
    "\\begin{equation*}\n",
    "    \\vec{P_0} =\n",
    "    \\begin{pmatrix}\n",
    "        6.5 - 6.5 \\\\\n",
    "        3.017716535 - 3\n",
    "    \\end{pmatrix} =\n",
    "    \\begin{pmatrix}\n",
    "        0 \\\\\n",
    "        0.017717\n",
    "    \\end{pmatrix}\n",
    "\\end{equation*}\n",
    "\n",
    "Let us also assume the user has placed another 0402 footprint with footprint center point $(6.6, 3.2)$ and selected pad center point $(6.582283465, 3.2)$. A similar vector, $\\vec{P_1}$, can be computed:\n",
    "\n",
    "\\begin{equation*}\n",
    "    \\vec{P_1} =\n",
    "    \\begin{pmatrix}\n",
    "        6.582283465 - 6.6 \\\\\n",
    "        3.2 - 3.2\n",
    "    \\end{pmatrix} =\n",
    "    \\begin{pmatrix}\n",
    "        -0.01772 \\\\\n",
    "        0\n",
    "    \\end{pmatrix}\n",
    "\\end{equation*}\n",
    "\n",
    "This yields the following vector line equations, in the form $\\vec{B} = \\vec{P_i} + \\vec{A}t$:\n",
    "\n",
    "\\begin{equation*}\n",
    "    \\vec{v} =\n",
    "    \\begin{pmatrix}\n",
    "        6.5 \\\\\n",
    "        3\n",
    "    \\end{pmatrix} +\n",
    "    \\begin{pmatrix}\n",
    "        0 \\\\\n",
    "        0.017717\n",
    "    \\end{pmatrix}t\n",
    "\\end{equation*}\n",
    "\n",
    "\\begin{equation*}\n",
    "    \\vec{w} =\n",
    "    \\begin{pmatrix}\n",
    "        6.6 \\\\\n",
    "        3.2\n",
    "    \\end{pmatrix} +\n",
    "    \\begin{pmatrix}\n",
    "        -0.01772 \\\\\n",
    "        0\n",
    "    \\end{pmatrix}u\n",
    "\\end{equation*}\n",
    "\n",
    "# Intersection Point\n",
    "The intersection point of both lines occurs when the $(x, y)$ coordinates are equal. Let us compute this by hand first in order to understand what is happening:\n",
    "\n",
    "\\begin{equation*}\n",
    "    6.5 + (0)t = 6.6 + (-0.01772)u \\\\\n",
    "    3 + (0.017717)t = 3.2 + (0)u \\\\ \\\\\n",
    "    \\Rightarrow t = 5.64429644, \\text{ } u = 11.28668172\n",
    "\\end{equation*}\n",
    "\n",
    "# Python Implementation\n",
    "Now let us have Python solve for the intersection point. To do this, it is helpful to represent the equations in classic $\\mathbf{A}x = \\mathbf{B}$ form first, in which we want to solve for $x$:\n",
    "\n",
    "\\begin{equation*}\n",
    "    a_1 = (0)t - (-0.01772)u \\\\\n",
    "    a_2 = (0.017717)t - (0)u \\\\\n",
    "    \\therefore \\vec{A} =\n",
    "    \\begin{pmatrix}\n",
    "        0 & 0.01772 \\\\\n",
    "        0.017717 & 0\n",
    "    \\end{pmatrix} \\\\\n",
    "    b_1 = w_x - v_x \\\\\n",
    "    b_2 = w_y - v_y \\\\\n",
    "    \\therefore \\vec{B} = \n",
    "    \\begin{pmatrix}\n",
    "        6.6 - 6.5 \\\\\n",
    "        3.2 - 3\n",
    "    \\end{pmatrix} = \n",
    "    \\begin{pmatrix}\n",
    "        0.1 \\\\\n",
    "        0.2\n",
    "    \\end{pmatrix}\n",
    "\\end{equation*}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([11.28859288,  5.64334086])"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.array([[0, 0.01772], [0.017717, 0]])\n",
    "b = np.array([(6.6-6.5), (3.2-3)])\n",
    "\n",
    "x = np.linalg.solve(a, b)\n",
    "x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Final Intersection Point\n",
    "While the above Python solution satisfies the solution for $x$, it is not our final intersection point. Next, we must substitute our solution in for either $\\vec{v}$ or $\\vec{w}$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "t = x[0]\n",
    "u = x[1]\n",
    "\n",
    "v = np.array([6.5, 3]) + np.array([0, 0.017717])*t\n",
    "w = np.array([6.6, 3.2]) + np.array([-0.01772, 0])*u"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([6.5, 3.2])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "v"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([6.5, 3.2])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Thus, the two vectors $\\vec{v}$ and $\\vec{w}$ intersect at the point $(6.5, 3.2)$."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
